{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 229,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "['abc', 1]\n"
     ]
    }
   ],
   "source": [
    "#Problem 3-1\n",
    "from ctypes import py_object#调用C语言\n",
    "class DynamicArray(object):\n",
    "    def __init__(self):\n",
    "        self._n=0\n",
    "        self._capacity=1\n",
    "        self._A=self._make_array(self._capacity)\n",
    "    def _make_array(self,c):\n",
    "        return(c * py_object)()\n",
    "    \n",
    "    def __len__(self):\n",
    "        return self._n\n",
    "    \n",
    "    def __getitem__(self,k):\n",
    "        if not 0<=k<self._n:\n",
    "            raise IndexError('invalid index')\n",
    "        return self._A[k]\n",
    "    \n",
    "    def _resize(self,new_capacity):\n",
    "        B=self._make_array(new_capacity)\n",
    "        for k in range(self._n):\n",
    "            B[k]=self._A[k]\n",
    "        self._A=B\n",
    "        self._capacity=new_capacity\n",
    "        \n",
    "    def append(self,value):\n",
    "        if self._n ==self._capacity:\n",
    "            self._resize(2*self._capacity)\n",
    "        self._A[self._n]=value\n",
    "        self._n+=1\n",
    "        \n",
    "    def insert(self,k,value):\n",
    "        if self._n==self._capacity:\n",
    "            self._resize(2*self._capacity)\n",
    "        for j in range(self._n,k,-1):\n",
    "            self._A[j]=self._A[j-1]\n",
    "        self._A[k]=value\n",
    "        self._n+=1\n",
    "            \n",
    "    def pop(self,k=None):\n",
    "        if k==None:\n",
    "            value=self._A[self._n-1]\n",
    "            self._A[self._n-1]=None\n",
    "            self._n-=1\n",
    "            return value\n",
    "        else:\n",
    "            if not 0<=k<self._n:\n",
    "                raise IndexError('invalid index')\n",
    "            value=self._A[k]\n",
    "            for j in range(k+1,self.n):\n",
    "                self._A[j-1]=self._A[j]\n",
    "            self._A[self._n-1]=None\n",
    "            self._n-=1\n",
    "            return value\n",
    "    def __repr__(self):\n",
    "        return f'{self._A[0:self._n]}'\n",
    "    \n",
    "if __name__=='__main__':\n",
    "    a=DynamicArray()\n",
    "    print(a)\n",
    "    a.append('abc')\n",
    "    a.append(1)\n",
    "    print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6, 3, 5, 2, 5, 4, 1]\n",
      "duplicate element in [6, 3, 5, 2, 5, 4, 1] is 5\n"
     ]
    }
   ],
   "source": [
    "#Problem 3-2\n",
    "import random\n",
    "def sample(n):\n",
    "    tmp=[i for i in range(1,n+1)]\n",
    "    tmp[n-1]=random.choice(tmp[1:-2])\n",
    "    random.shuffle(tmp)\n",
    "    array=DynamicArray()\n",
    "    for i in tmp:\n",
    "        array.append(i)\n",
    "    return array\n",
    "\n",
    "def find_duplicate(array):\n",
    "    num = int(sum([i for i in array._A[0:array._n]])-(array._n-1)*array._n/2)\n",
    "    return num\n",
    "\n",
    "if __name__=='__main__':\n",
    "    test=sample(7)\n",
    "    print(test)\n",
    "    print(f'duplicate element in {test} is {find_duplicate(test)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
